/*
*  去左右空格
*/
function trim(s){
 	return rtrim(ltrim(s)); 
}
/*
*  去左空格
*/
function ltrim(s){
 	return s.replace( /^\s*/, ""); 
}
/*
*  去右空格
*/ 
function rtrim(s){ 
 	return s.replace( /\s*$/, ""); 
}
/*
*  验证所填内容长度
*/
function limitLen(s,Min,Max){
var ss = s.value;
	ss=trim(ss);
	if(ss=="") return false;
	if((ss.length<Min)||(ss.length>Max)){
		return "_error_";}
	else{
		return "__ok__";}
}
/*
*  验证所填颜色值是否为正确格式
*/ 
function isColor(ss){ 
	var s = ss.value;
	s = trim(s); 
    if(s.length<1){
       return "_error_";
    }
	if (s.length !=7){
        return "_error_"; 
    }
    if(!(s.search(/\#[a-fA-F0-9]{6}/) != -1)){
       return "_error_"; 
    }
	return "__ok__"; 
}
/*
*  验证所填身份证是否为正确格式
*/
function isCard(ss){
	var s = ss.value;
    if(s.length<1){
       return "_error_";
    }
	s = trim(s); 
	var p = /^(\d{15}|\d{17}[x0-9])$/i;
    if(!p.test(s)){
       return "_error_";
    }
	return "__ok__";
}
/*
*  验证所填URL是否为正确格式
*/
function isURL(ss){
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    }
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)" 
    + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" 
          + "(([0-9]{1,3}\.){3}[0-9]{1,3}"
          + "|" 
          + "([0-9a-z_!~*'()-]+\.)*" 
          + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."  
          + "[a-z]{2,6})" 
          + "(:[0-9]{1,4})?" 
          + "((/?)|"
          + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
          var re=new RegExp(strRegex); 
          if (!re.test(s)){
              return "_error_"; 
          }
          return "__ok__";
      }
/*
*  验证所填Zip是否为正确格式
*/
function isZip(ss){
var s = ss.value;
    if(s.length<1){
       return "_error_";
    }
	s = trim(s);
	var p = /^[1-9]\d{5}$/;
	if(!p.test(s)){
       return "_error_";
    }
	return "__ok__";
}
/*
*  验证所填内容是否为Double类型数据
*/
function isDouble(ss){
var s = ss.value;
    if(s.length<1){
       return "_error_";
    }
	s = trim(s);
	var p = /^[-\+]?\d+(\.\d+)?$/;
	if(!p.test(s)){
       return "_error_";
    }
	return "__ok__";
}
/*
*  验证所填内容是否含有中文字符
*/
function hasChineseChar(ss){
var s = ss.value;
	var p = /[^\x00-\xff]/;
	return p.test(s);
}
/*
*  数字、英文、下划线、中杠线
*/
function hasAccountChar(ss){
var s = ss.value;
    if(s.length<1){
       return "_error_";
    }
	var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,15}$/;
	if(!p.test(s)){
       return "_error_";
    }
	return "__ok__";
}
/*
*  验证所填内容每位是否为双字节
*/
function isDoubleChar(ss){
var s = ss.value;
	var p = /^[^\x00-\xff]+$/;
	return p.test(s);
}
/*
*  验证所填内容每位是否为中文
*/
function isChinese(ss){
var s = ss.value;
    if(s.length<1){
       return "_error_";
    }
     var regu = /^[\u0391-\uFFE5·]+$/;
     if(!regu.test(s)){
         return "_error_";
     }
     return "__ok__";        
}
/*
*  验证所填内容每位是否为英文
*/
function isEnglish(ss){
var s = ss.value;
	var p = /^[A-Za-z]+$/;
	s = trim(s);
	if(s.length<1){
       return "_error_";
    }
	if(!p.test(s)){
       return "_error_";
    }
    return "__ok__";
}
/*
*  验证所填ip地址格式是否正确
*/
function isIP(ss) {
var s = ss.value;
    if(s.length < 1){
       return "_error_"; 
    }
    var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式 
    if(re.test(s)) { 
       if(RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
          return "__ok__";
       }else{
          return "_error_";
       } 
    }else{
       return "_error_";
    }
} 
/*
*  检查输入字符串是否为空或者全部都是空格 
*/
function isNull(a){
	var s = a.value;
    if (s == ""){
        return "_error_";
    } 
    var regu = "^[ ]+$"; 
    var re = new RegExp(regu); 
    if(re.test(s)){
       return "_error_";
    }
    return "__ok__";
} 
/*
*  检查输入对象的值是否符合整数格式 
*/
function isInteger(ss){
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    } 
    var regu = /^[-]{0,1}[0-9]{1,}$/; 
    if(!regu.test(s)){
       return "_error_";
    }
    return "__ok__";
} 
/*
*  检查输入手机号码是否正确  
*/ 
function checkMobile(ss){
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    }
    var regu =/^[1][3][0-9]{9}$/; 
    var re = new RegExp(regu); 
    if (!re.test(s)) { 
        return "_error_";
    }
    return "__ok__"; 
} 
/*
*  检查输入字符串是否符合正整数格式
*/ 
function isNumber(ss){ 
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    }
    var regu = "^[0-9]+$"; 
    var re = new RegExp(regu); 
    if (s.search(re) != -1) { 
        return "__ok__"; 
    }else{
       return "_error_"; 
    }
} 
/*
*  检查输入字符串是否是带小数的数字格式,可以是负数
*/ 
function isDecimal(ss){ 
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    } 
    var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/; 
    if(re.test(s)){ 
       if(RegExp.$1==0&&RegExp.$2==0){
          return "_error_"; 
       }else{
          return "__ok__";
       }
    }else{
       return "_error_"; 
    } 
} 
/*
*  检查输入对象的值是否符合端口号格式 
*/
function isPort(ss){
var s = ss.value;
    if(s.length < 1){
       return "_error_";
    } 
    var regu = "^[0-9]+$"; 
    var re = new RegExp(regu); 
    if (s.search(re) != -1&&s<65536) {
       return "__ok__";
    }
    return "_error_";
} 
/*
*  检查输入对象的值是否符合E-Mail格式 
*/ 
function isEmail(ss){ 
var s = ss.value;
    if(s.length < 5) {
       return "_error_";
    }
    s = trim(s); 
 	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i; 
    if(!p.test(s)) {
       return "_error_";
    }
    return "__ok__";
       
} 
/*
*  检查输入字符串是否符合金额格式,格式定义为带小数的正数，小数点后最多三位
*/ 
function isMoney(ss){
var s = ss.value;
    if(s.length < 1) {
       return "_error_";
    }
    var regu = "^[0-9]+[\.][0-9]{0,3}$"; 
    var re = new RegExp(regu); 
    if (!re.test(s)) { 
        return "_error_"; 
    }
    return "__ok__"; 
}
/*
*  检查输入字符串是否只由英文字母和数字和下划线组成 
*/  
function isNumberOr_Letter(ss){
	var s = ss.value;
    if(s.length < 1) {
       return "_error_";
    } 
    var regu = "^[0-9a-zA-Z\_]+$"; 
    var re = new RegExp(regu); 
    if (!re.test(s)) {
        return "_error_"; 
    }
    return "__ok__"; 

}
/*
*  检查输入字符串是否只由英文字母和数字组成 
*/   
function isNumberOrLetter(ss){
	var s = ss.value;
    if(s.length < 1) {
       return "_error_";
    } 
    var regu = "^[0-9a-zA-Z]+$"; 
    var re = new RegExp(regu); 
    if (!re.test(s)) { 
        return "_error_"; 
    }
    return "__ok__"; 
}
/*
*  检查输入字符串是否只由汉字、字母、数字组成 
*/  
function isChinaOrNumbOrLett(s){
	var ss = s.value;
    if(s.length < 1) {
       return "_error_";
    } 
    var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$"; 
    var re = new RegExp(regu); 
    if (!re.test(s)) {
        return "_error_"; 
    } 
    return "__ok__"; 

} 
/*
*  判断是否是日期  
*/   
function isDate(s,fmt) { 
    if(s.length < 1) {
       return false;
    } 
    if(fmt==null){
       fmt="yyyyMMdd"; 
    }
    var yIndex = fmt.indexOf("yyyy");
    if(yIndex==-1){
       return false;
    }
    var year = s.substring(yIndex,yIndex+4); 
    var mIndex = fmt.indexOf("MM"); 
    if(mIndex==-1){
       return false;
    } 
    var month = s.substring(mIndex,mIndex+2); 
    var dIndex = fmt.indexOf("dd"); 
    if(dIndex==-1){
       return false;
    } 
    var regu = "^[0-9]+$"; 
    var re = new RegExp(regu); 
    var day = s.substring(dIndex,dIndex+2); 
    if(!(year.search(re) != -1)||year>"2100" || year< "1900"){
       return false;
    } 
    if(!(month.search(re) != -1)||month>"12" || month< "01"){
       return false;
    } 
    if(day>getMaxDay(year,month) || day< "01"){
       return false;
    } 
    return true; 
} 
/*
*  获取任一个月中的最大天数  
*/ 
function getMaxDay(year,month) { 
    if(month==4||month==6||month==9||month==11){ 
       return "30";
    } 
    if(month==2){ 
      if(year%4==0&&year%100!=0 || year%400==0){ 
         return "29"; 
      }else{ 
         return "28";
      } 
    }
    return "31"; 
}
/*
*  判断是否是日期   
*/  
function isDate1(s,fmt) { 
    if(fmt==null){
       fmt="yyyyMMdd"; 
    }
    var yIndex = fmt.indexOf("yyyy");
    if(yIndex==-1){
       return false;
    }
    var year = s.substring(yIndex,yIndex+4); 
    var mIndex = fmt.indexOf("MM"); 
    if(mIndex==-1){
       return false;
    } 
    var month = s.substring(mIndex,mIndex+2); 
    var dIndex = fmt.indexOf("dd"); 
    if(dIndex==-1){
       return false;
    } 
    var regu = "^[0-9]+$"; 
    var re = new RegExp(regu); 
    var day = s.substring(dIndex,dIndex+2); 
    if(!(year.search(re) != -1)||year>"2100" || year< "1900"){
       return false;
    } 
    if(!(month.search(re) != -1)||month>"12" || month< "01"){
       return false;
    } 
    if(day>getMaxDay(year,month) || day< "01"){
       return false;
    }
    return true; 
} 
/*
*  获取任一个月中的最大天数  
*/ 
function getMaxDay(year,month) { 
    if(month==4||month==6||month==9||month==11){ 
       return "30";
    } 
    if(month==2){ 
      if(year%4==0&&year%100!=0 || year%400==0){ 
         return "29"; 
      }else{ 
         return "28";
      } 
    }
    return "31"; 
}
/*
*  字符1是否以字符串2结束 
*/ 
function isLastMatch(str1,str2) { 
    var index = str1.lastIndexOf(str2); 
    if(str1.length==index+str2.length) return true; 
    return false; 
}
/*
*  字符1是否以字符串2开始 
*/  
function isFirstMatch(str1,str2) { 
    var index = str1.indexOf(str2); 
    if(index==0) return true; 
    return false; 
}
/*
*  字符1是包含字符串2
*/  
function isMatch(str1,str2) { 
    var index = str1.indexOf(str2); 
    if(index==-1) return false; 
    return true; 
}
/*
*  检查输入的起日期是否正确
*/
function checkTwoDate1(s) {
     if(s.length < 1) {
       return false;
     } 
     if(!isDate1(s)){
        return false;
     } 
    return true; 
} 
/*
*  检查输入的止日期是否正确，规则为两个日期的格式正确， 
*/ 
function checkTwoDate2(s,date1) {
     if(s.length < 1) {
       return false;
     } 
     if(!isDate1(s)){
        return false;
     }
     if($F(date1) > s){ 
      return false; 
    }
    return true; 
}
/*
*  检查输入的电话号码格式是否正确
*/ 
function checkPhone(ss){ 
    var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/; 
    var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/; 
    var s = ss.value;
    if(s.length < 1){
       return "_error_"; 
    }
    if(s.length > 9){
       if(phoneRegWithArea.test(s)){
          return "__ok__"; 
       }else{
          return "_error_"; 
       }
    }else{
       if(phoneRegNoArea.test(s)){
          return __ok__; 
       }else{
          return "_error_"; 
       }
    }
}
/*
*  检查输入的密码格式是否正确
*/
function checkPwd(s,min,max,inputId){
		if(!limitLen(s,min,max)){
			return false;
		}
		if(hasChineseChar(s)){
			return false;
		}
		if(inputId!=null){
		if(limitLen($F(inputId),min,max)){
			if(trim($F(inputId)) == trim(s)){
				return true;
			}else{
				return false;
			}
		}
		}
		return true;
	}
/*
*  检查输入的确认密码格式是否正确和密码是否一致
*/
function checkPwd2(s,min,max,inputId){
		if(!limitLen(s,min,max)){
			return false;
		}
		if(hasChineseChar(s)){
			return false;
		}
		if(limitLen($F(inputId),min,max)){
			if(trim($F(inputId)) == trim(s)){
				return true;
			}else{
				return false;
			}
		}
		return true;
	}
/*
*  显示信息
*/
function showInfo(n,s){
        var fdo = $(n+"FrameDiv");
		//var ido = $(n+"InfoDiv");
		if(typeof s == 'undefined'){
            fdo.className = "FrameDivPass";
			fdo.innerHTML = "";
		}else{
		    fdo.className = "FrameDivError";
			fdo.innerHTML = s;
		}
	}
	
	function showWarnInfo(n,s){
        var fdo = $(n+"FrameDiv");
		//var ido = $(n+"InfoDiv");
		if(typeof s == 'undefined'){
            fdo.className = "FrameDivPass";
			fdo.innerHTML = "";
		}else{
		    fdo.className = "FrameDivWarn";
			fdo.innerHTML = s;
		}
	}

function ajaxValidate(id, xml, responseMethod, methods, isSubmit, submitMethod){
	var object = $(id);
	var pars = "value=" + escape(object.value) + "&xml=" + xml;
//alert("id " + object.value + "\nxml" + xml + "\n" + methods + "\n" + isSubmit + submitMethod);
	doAjaxValidate(pars, responseMethod, methods, isSubmit, submitMethod, id, "error");
}

function ajaxValidateWarn(id, xml, responseMethod, methods, isSubmit, submitMethod) {
	var object = $(id);
	var pars = "value=" + escape(object.value) + "&xml=" + xml;
//	alert("id " + object.value + "\nxml" + xml + "\n" + methods + "\n" + isSubmit + submitMethod);
	doAjaxValidate(pars, responseMethod, methods, isSubmit, submitMethod, id, "warn");
}

//发送ajax验证请求
function doAjaxValidate(pars, responseMethod, methods, isSubmit, submitMethod, id, type) {
	var url = 'saturn.validate';  
	var map = new HashMap();
	map.put("methods",methods);
	map.put("isSubmit",isSubmit);
	map.put("submitMethod",submitMethod);
	map.put("id", id);
	map.put("type", type);
	var myAjax = new Ajax.SaturnRequest(
 	 	url,                    
 	 	{
 	 	method: 'post', 
 	 	parameters: pars, 
 	 	onComplete: responseMethod,
 	 	args:map
 	});  
}

function responseMethod(response,map){ 
    var root = response.responseXML.documentElement;
	var info = root.getElementsByTagName("value");
    var isSubmit = map.get("isSubmit");
    var submitMethod = map.get("submitMethod");
    var id = map.get("id");
    var methods = map.get("methods");
    var type = map.get("type");
    
    if (info[1].firstChild.nodeValue != "true") {
    	if (errorInfos) {
    		errorInfos.addErrorInfo(id, type, info[1].firstChild.nodeValue); 
    	}
    }
    
    if (info[1].firstChild.nodeValue == "true" || isSubmit){
    	if (info[1].firstChild.nodeValue=="true") {
    		showInfo(id);
    	}
        if (methods && methods.length > 1){
        	callNext(methods, isSubmit, submitMethod, id);
        } else{
        	if(isSubmit){
        		_system_validate_do_submit(submitMethod);
        	} else {
            	showInfo(id);
            }
        }
    } else {
    	if (type == "error") {
        	showInfo(id, info[1].firstChild.nodeValue);	
        } else {
        	showWarnInfo(id, info[1].firstChild.nodeValue);
        }
    }
}

function callNext(methods, isSubmit, submitMethod,id) {
	if (methods && methods.length > 1){
		methods.remove(0);
	    var fieldname = methods[0].split('_')[0];
	    eval(methods[0]+"('" + fieldname + "','true',methods,isSubmit, submitMethod);");
     } else{
        	if(isSubmit){
        		_system_validate_do_submit(submitMethod);
        	} else {
            	showInfo(id);
            }
        }
}
var ErrorInfos = function(){
	this.errorInfo = new HashMap();
	this.hasWarn = false;
	this.hasError = false;
};
ErrorInfos.prototype = {
	setErrorInfos:function(property,type,messages){
		if (this.errorInfo.get(property)){
			if (this.errorInfo.get(property) =="error" && type != "warn") {
				this.errorInfo.get(property).setMessages(messages);
			}
		} else {
			var error = new validateVO();
			error.setId(property);
			error.setType(type);
			error.setMessages(messages);
			this.errorInfo.put(property,error);
			this.initalErrorType(type);
		}
	},
	setError:function(property,error){
		this.errorInfo.put(property,error);
	},
	addErrorInfo:function(property,type,message){
		if(this.errorInfo.get(property)){
			if( this.errorInfo.get(property) == "error" && type != "warn") {
				this.errorInfo.get(property).setMessage(message);
			}
		}else{
			var error = new validateVO();
			error.setId(property);
			error.setType(type);
			error.setMessages(message);
			this.errorInfo.put(property,error);
			this.initalErrorType(type);
		}
	},
	getErrorInfos:function(){
		return this.errorInfo;
	},
	getErrorInfo:function(property){
		return this.errorInfo.get(property);
	},
	getProperties:function(){
		return this.errorInfo.getKeys();
	},
	initalErrorType:function(type) {
		if (type == "error") {
			if (this.hasError == false) {
				this.hasError = true;
			}
		} 
		
		if (type == "warn") {
			if (this.hasWarn == false && this.hasError ==false) {
//		if (this.hasWarn == false) {
				this.hasWarn = true;
			}
		} 
	},
	isHasError:function() {
		return this.hasError;
	},
	isHasWarn:function() {
		return this.hasWarn;
	},
	getAllErrors:function() {
		var keys = this.errorInfo.getKeys();
		var errors = new Array();
		for (var i = 0; i < keys.length; ++i) {
			var errorInfo = errorInfos.getErrorInfo(keys[i]);
			if (errorInfo.getType() == "error") {
				errors.push(errorInfo);
			}
		}
		
		return errors;
	},
	getAllWarns:function() {
		var keys = this.errorInfo.getKeys();
		var warns = new Array();
		for (var i = 0; i < keys.length; ++i) {
			var errorInfo = errorInfos.getErrorInfo(keys[i]);
			if (errorInfo.getType() == "warn") {
				warns.push(errorInfo);
			}
		}
		return warns;
	}			
};
//校验信息模型
var validateVO = function(){
	this.id = null;
	this.type = null;
	this.messages = new Array();
};
validateVO.prototype = {
	setId:function(id){
		this.id = id;
	},
	setType:function(type){
		this.type = type;
	},
	setMessage:function(message){
		this.messages.push(message);
	},
	setMessages:function(messages){
		this.messages = (new Array()).concat(messages);
	},
	getId:function(){
		return this.id;
	},
	getType:function(){
		return this.type;
	},
	getMessages:function(){
		return this.messages;
	},
	getMessage:function(index){
		return this.messages[index];
	},
	size:function(){
		return this.messages.length;
	}
};
function _system_validate_do_submit(submitMethod){
	var properties = errorInfos.getProperties();
	var hasErrors = errorInfos.isHasError();
	var hasWarns = errorInfos.isHasWarn();
	var warnInfos = new Array();
	if(hasErrors){
		for(var i=0; i< properties.length; i++){
			var errorInfo = errorInfos.getErrorInfo(properties[i]);
			if(errorInfo.getType()=="error"){
				showInfo(properties[i],errorInfo.getMessage(0));
			}
		}
	}else if(hasWarns){
		var warnMessagesStr = "";
		
		var allWarns = errorInfos.getAllWarns();
		for(var i=0; i< allWarns.length; i++) {
			var errorInfo = allWarns[i];
			for(var j=0;j<errorInfo.size();j++){
				warnMessagesStr += errorInfo.getMessage(j)+"\n";
			}
		}
		if(window.confirm(warnMessagesStr+"\n 是否调整?")){
			for(var i=0; i< allWarns.length; i++) {
				var errorInfo = allWarns[i];
				showWarnInfo(errorInfo.getId(),errorInfo.getMessage(0));
			}
		}else{
			eval(submitMethod+"();");
		}
	}else{
		//alert("do submit , have not error and warn");
		eval(submitMethod+"();");
	}
};
function getValidatePropertyValue(id){
	if(document.getElementsByName(id)[0]){
		return $F(id);
	}else{
		return "";
	}
};
